$(document).ready(function(){
	//绑定返回顶部按钮
	BC.returnTop('#toTop',300,100);
	//星星评分
	var starTips = ['垃圾','没啥感觉','还过得去','挺不错的','太好看啦'],
		starWidth = 15,
		tempTips = '',
		$giveScore = $('.give-score'),
		$scoreSelected = $('.score-selected'),
		$scoreTips = $('.score-tips'),
		$giveScoreLabel = $('.give-score-label'),
		$delScore = $('.del-score'),
		//评论表单提交
		$commentForm = $('#comment-form'),
		$comment = $('#comment'),
		$commentBtn = $('.comment-btn'),
		$biComment = $('.b-i-comment'),
		floorLength = $('.b-i-c-content').length,
		//我要借按钮
		$debitBtn = $('.debit-book-btn'),
		//借书申请内容
		$dialogBox = $('#book-info-dialog-box'),
		$sendTipsForm = $('#send-tips-form'),
		isBind = false;
	$giveScore.find('a').each(function(i){
		//绑定评价事件
		$(this).click(function(){
			var n = i+1;
			BC.ajax({
				type:'POST',
				url:'/bookcrossing/book/bookComment',
				data:{bid:$('.hidden-bid').val(),score:n*2},
				s:function(resp){
					/*$scoreSelected.css('width',n*starWidth+'px');
					tempTips = starTips[i];
					$scoreTips.text(tempTips);
					if($giveScoreLabel.text!='我的评价：'){
						$giveScoreLabel.text('我的评价：');
					}
					$delScore.show();*/
					window.location.reload();
				},
				f:function(resp){
					if(!resp.isLogin){
						if(confirm('请先登录哦')){
							window.location.href=BC.getRootPath()+'login?back=1';
						}
					}else{
						alert(resp.msg);
					}
				},
				e:function(){
					
				}
			});
		});
		$(this).hover(
			function(){
				if($.trim($scoreTips.text())!=''){
					tempTips = $scoreTips.text();
				}
				$scoreTips.text(starTips[i]);
			},
			function(){
				if(tempTips!=''){
					$scoreTips.text(tempTips);
				}else{
					$scoreTips.text('');
				}
			}
		);
	});
	//绑定删除评价事件
	$delScore.click(function(){
		if(confirm('确定删除你的评价吗')){
			BC.ajax({
				type:'GET',
				url:'/bookcrossing/book/BookDeleteScoreOrComment',
				data:{bid:$('.hidden-bid').val()},
				s:function(resp){
					/*$giveScoreLabel.text('评价：');
					$scoreSelected.css('width','0px');
					tempTips = '';
					$scoreTips.text('');
					$delScore.hide();*/
					window.location.reload();
				},
				f:function(resp){
					alert(resp.msg);
				}
			});
		}
	});
	//绑定我要借事件
	$debitBtn.click(function(){
		BC.ajax({
			type:'GET',
			url:'/bookcrossing/isLogin',
			data:{},
			s:function(resp){
				$dialogBox.modal({
					backdrop:'static',
					keyboard:false
				});
				if(!isBind){
					$sendTipsForm.submit(function(e){
						e.preventDefault();
						BC.ajax({
							type:'POST',
							url:'/bookcrossing/sendTips',
							data:$sendTipsForm.serialize()+'&bid='+$('.hidden-bid').val(),
							s:function(resp){
								$dialogBox.find('.modal-content').html(
									'<div class="modal-body" style="padding-bottom:20px"><span class="tips-pic pic-success"></span><span class="tips-content"> '+resp.msg+'</span><br></div>'
								);
								$dialogBox.on('hidden.bs.modal',function(e){
									window.location.reload();
								});
								window.setTimeout(function(){
									$dialogBox.modal('hide');
								},800);
							},
							f:function(resp){
								alert(resp.msg);
							}
						});
					});
					isBind = true;
				}
			},
			f:function(resp){
				if(!resp.isLogin){
					if(confirm('请先登录哦')){
						window.location.href=BC.getRootPath()+'login?back=1';
					}
				}
			}
		});
	});
	//评论表单提交
	$commentForm.submit(function(e){
		e.preventDefault();
		if($.trim($comment.val())==''){
			alert('写点什么再提交呀 骚年');
			$comment.val('');
			$comment.focus();
		}else{
			BC.ajax({
				type:'POST',
				url:'/bookcrossing/book/bookComment',
				data:{bid:$('.hidden-bid').val(),bccontents:$.trim($comment.val())},
				b:function(){
					$commentBtn.html('<span class="loading-pic"></span>').attr('disabled','disabled');
				},
				s:function(resp){
					var $commentParse = $(parseCommentHtml(resp));
					$commentParse.hide();
					$biComment.append($commentParse);
					$commentParse.slideDown('normal');
					$comment.val('');
				},
				f:function(resp){
					if(!resp.isLogin){
						if(confirm('请先登录哦')){
							window.location.href=BC.getRootPath()+'login?back=1';
						}
					}else{
						alert(resp.msg);
					}
				},
				c:function(){
					$commentBtn.html('评论').removeAttr('disabled');
				}
			});
		}
	});
	//绑定删除评论事件
	$(document).delegate('.del-btn','click',function(e){
		if(confirm('确定删除该评论吗')){
			BC.ajax({
				type:'GET',
				url:'/bookcrossing/book/BookDeleteScoreOrComment',
				data:{bcid:$(e.target).attr('data-bcid')},
				s:function(resp){
					$(e.target).closest('.b-i-c-content').slideUp(function(){
						$(this).remove();
					});
				},
				f:function(resp){
					alert(resp.msg);
				}
			});
		}
	});
	
	//输入框ctrl+enter提交评论事件
	var timer = null;
	$comment.keydown(function(e){
		if(timer){
			window.clearTimeout(timer);
		}
		timer = window.setTimeout(function(){
			if(e.ctrlKey && e.which==13 || e.which == 10){
				$commentBtn.click();
			}
		},30);
	});
	
	//拼装评论成功后返回的内容
	function parseCommentHtml(data){
		var str ='',
			intro = ' ',
			starsStr = '';
			stars = parseInt($('.score-selected').css('width'));
		if(data.introduce!='' && data.introduce!='null'){
			intro += '('+data.introduce+')';
		}
		if(stars!=0){
			starsStr = '<span class="smallstar sm-star-'+stars/15+'" title="'+starTips[stars/15-1]+'"></span>';
		}
		var closeBtn = '<button type="button" class="close del-btn" data-bcid="'+data.bcid+'" title="删除评论">&times;</button>',
			floor = '<span class="comment-floor">#'+(++floorLength)+'</span>';
		str += '<div class="b-i-c-content"><div class="b-i-c-c-left"><img src="/bookcrossing/img/user/'+data.upic+'" alt="头像"/></div>';
		str += '<div class="b-i-c-c-right"><p class="b-i-c-c-r-top"><a href="/bookcrossing/user/profile?uid='+data.uid+'" target="_blank">'+data.uid+'</a>';
		str += '<span class="b-i-c-c-r-introduce">'+intro+'</span> '+starsStr+'</p>';
		str += '<p class="b-i-c-c-r-com">'+$.trim($comment.val())+'</p><p class="b-i-c-c-r-time">'+data.bctime+'</p></div>';
		str += closeBtn+floor+'</div>';
		return str;
	}
});




